package com.yunhe.sms.controller.active;

import com.yunhe.commens.dto.JsonResponce;
import com.yunhe.commens.dto.Pagedao;
import com.yunhe.sms.entity.activity.Vote;
import com.yunhe.sms.service.activite.VoteService;
import com.yunhe.sms.service.activite.impl.VoteServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author LeiPeifeng
 * @version V1.0
 * @date 2017/10/30 0030 19:11
 * @Description: 投票控制
 */
@Controller
@RequestMapping("/vote")
public class VoteController {
    @Autowired
    private VoteService voteService ;

    /**
     * 列表
     * @return
     */
    @RequestMapping("/list")
    public String list(){
        return "active/vote/list";
    }

    /**
     * 添加页面
     * @return
     */
    @RequestMapping("/add")
    public String add(){
        return "active/vote/add";
    }

    /**
     * 编辑页面
     * @return
     */
    @RequestMapping("/edit")
    public String edit(HttpServletRequest req,Long id){
            Vote vote = voteService.findId ( id );
            req.setAttribute ( "vote",vote );
        return "active/vote/edit";
    }

    /**
     * 删除
     * @return
     */
    @RequestMapping("/deleted")
    @ResponseBody
    public JsonResponce deleted(String ids){
        voteService.deleted ( ids );
        return new JsonResponce (  );
    }

    /**
     * 保存
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public JsonResponce save( Vote vote){
         voteService.add ( vote );
        return new JsonResponce ( 1,"创建成功!",vote.getId () );
    }

    /**
     * 修改
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public JsonResponce update( Vote vote){
        voteService.update ( vote );
        return new JsonResponce ( 1,"修改成功!");
    }

    @RequestMapping("/page")
    @ResponseBody
    public Pagedao<Vote> page(int offset, int limit, HttpServletResponse resp){
       resp.setCharacterEncoding ( "utf-8" );
        Pagedao<Vote> page = voteService.page ( offset, limit );
        return page;
    }
    @RequestMapping("/upstatus")
    @ResponseBody
    public JsonResponce upstatus(HttpServletResponse resp,Long id){
        resp.setCharacterEncoding ( "utf-8" );
        voteService.upstatus ( id );
        return new JsonResponce ( 1,"发布成功！" );
    }

}
